home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.02 Feb 90 / cdev Shell Code / cdev.p next >
Encoding:
Text File  |  1989-08-07  |  5.6 KB  |  220 lines  |  [TEXT/MPS ]

  1. UNIT cdev;
  2.  
  3. INTERFACE
  4.  
  5. USES    MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf;
  6.  
  7. FUNCTION Sample(message, Item, numItems, CPanelID: INTEGER; 
  8.                 VAR evt: EventRecord; cdevValue: LONGINT; 
  9.                 CPDialog: DialogPtr): LONGINT;    FORWARD;
  10.  
  11. IMPLEMENTATION
  12.  
  13. CONST
  14.         iVersion =            1;
  15.         iTitle =            2;
  16.         iShowCounts =         3;
  17.         iHideCounts =        4;
  18.         iTitleHandled =        5;
  19.         iTitleIgnored =        6;
  20.         iHandled =            7;
  21.         iIgnored =            8;
  22.  
  23. TYPE
  24.     SampleStorage = RECORD
  25.         dlgPtr:        DialogPtr;
  26.         dlgItems:    integer;
  27.         countShown:    Boolean;
  28.         msgHandled:    integer;
  29.         msgIgnored:    integer;
  30.     END;
  31.     SamplePtr = ^SampleStorage;
  32.     SampleHdl = ^SamplePtr;
  33.  
  34. FUNCTION InitSample(CPDialog : DialogPtr; numItems: integer): longInt;    FORWARD;
  35. FUNCTION EnoughRoomToRun(var cdevValue : longInt): Boolean;    FORWARD;
  36. PROCEDURE CountMessage(ourHandle: SampleHdl; handledIt: Boolean); FORWARD;
  37. PROCEDURE HitSample(ourHandle: SampleHdl; item: integer); FORWARD;
  38. PROCEDURE DrawSampleItem(ourHandle: SampleHdl; item: integer);    FORWARD;
  39. FUNCTION IGetCtlHand(ourHandle: SampleHdl; item: integer): ControlHandle;    FORWARD;
  40. PROCEDURE IGetRect(ourHandle: SampleHdl; item: integer; var itemRect: Rect);    FORWARD;
  41. PROCEDURE IHide(ourHandle: SampleHdl; item: integer);    FORWARD;
  42. PROCEDURE IShow(ourHandle: SampleHdl; item: integer);    FORWARD;
  43. PROCEDURE IInvalidate(ourHandle: SampleHdl; item: integer); FORWARD;
  44.  
  45. FUNCTION Sample(message, Item, numItems, CPanelID: INTEGER; 
  46.                 VAR evt: EventRecord; cdevValue: LONGINT; 
  47.                 CPDialog: DialogPtr): LONGINT;
  48. VAR i:                    integer;
  49.     handledIt:            Boolean;
  50.     ourHandle:            SampleHdl;
  51.     storageExpected:    Boolean;
  52. BEGIN
  53.     storageExpected := not ((message = initDev) or (message = macDev));
  54.     if storageExpected and ((cdevValue = 0) or (cdevValue = cdevUnset)) then
  55.     BEGIN
  56.         cdevValue := 0;
  57.     END
  58.     else if storageExpected and not EnoughRoomToRun(cdevValue) then
  59.             BEGIN
  60.             END
  61.     else
  62.         BEGIN
  63.             HandledIt := true;
  64.             ourHandle := SampleHdl(cdevValue);
  65.             case message of
  66.                 initDev:    if EnoughRoomToRun(cdevValue) then BEGIN
  67.                                 cdevValue := InitSample(CPDialog, numItems);
  68.                                 ourHandle := SampleHdl(cdevValue);
  69.                             END;
  70.                 closeDev:    if ourHandle <> nil then BEGIN
  71.                                 DisposHandle(Handle(ourHandle));
  72.                                 cdevValue := 0;
  73.                                 ourHandle := nil;
  74.                             END;
  75.                 hitDev:        HitSample(ourHandle, item - numItems);
  76.                 updateDev:    for i := iHandled to iIgnored do
  77.                                 DrawSampleItem(ourHandle,i);
  78.                 otherwise    handledIt := false;
  79.             END; {of case}
  80.             if ourHandle <> nil then
  81.                 CountMessage(ourHandle, handledIt);
  82.         END;
  83.     Sample := cdevValue;
  84. END;
  85.  
  86. FUNCTION InitSample(CPDialog : DialogPtr; numItems: integer): longInt;
  87. VAR    i:            integer;
  88.     ourHandle:    SampleHdl;
  89. BEGIN
  90.     ourHandle := SampleHdl( NewHandle(sizeof(SampleStorage)));
  91.     if ourHandle <> nil then BEGIN
  92.         with ourHandle^^ do BEGIN
  93.             dlgPtr := CPDialog;
  94.             dlgItems := numItems;
  95.             msgHandled := 0;
  96.             msgIgnored := 0;
  97.             countShown := true;
  98.         END; {with}
  99.         for i := iShowCounts to iHideCounts do
  100.             SetCtlValue(IGetCtlHand(ourHandle, i), ORD(i=iShowCounts));
  101.     END; {if}
  102.     InitSample := ORD4(ourHandle);
  103. END;
  104.  
  105. FUNCTION EnoughRoomToRun(var cdevValue : longInt): Boolean;
  106. VAR    error:        integer;
  107.     packHand:    Handle;
  108. BEGIN
  109.     EnoughRoomToRun := true;
  110.     Exit(EnoughRoomToRun);
  111.     packHand := RGetResource('PACK',7);
  112.     if packHand <> nil then BEGIN
  113.         EnoughRoomToRun := true;
  114.         Exit(EnoughRoomToRun)
  115.     END;
  116.  
  117.     if ResError = resNotFound then
  118.         error := cdevResErr
  119.      else
  120.         error := cdevMemErr;
  121.  
  122.     if (cdevValue <> cdevUnset) and (Handle(cdevValue) <> nil) then
  123.         DisposHandle(Handle(cdevValue));
  124.     cdevValue := error;
  125.     EnoughRoomToRun := false;
  126. END;
  127.  
  128.  
  129. PROCEDURE CountMessage(ourHandle: SampleHdl; handledIt: Boolean);
  130. BEGIN
  131.     if ourHandle <> nil then
  132.         with ourHandle^^ do
  133.             if handledIt then BEGIN
  134.                 msgHandled := msgHandled + 1;
  135.                 DrawSampleItem(ourHandle, iHandled);
  136.             END
  137.             else BEGIN
  138.                 msgIgnored := msgIgnored + 1;
  139.                 DrawSampleItem(ourHandle, iIgnored);
  140.             END
  141. END;
  142.  
  143. PROCEDURE HitSample(ourHandle: SampleHdl; item: integer);
  144. var    i:    integer;
  145. BEGIN
  146.     with ourHandle^^ do
  147.         if countShown <> (item = iShowCounts) then
  148.             countShown := (item = iShowCounts)
  149.         else
  150.             Exit(HitSample);
  151.     for i := iShowCounts to iHideCounts do
  152.         SetCtlValue(IGetCtlHand(ourHandle,i), ORD(i=item));
  153.     for i:= iTitleHandled to iIgnored do BEGIN
  154.         if item = iShowCounts then
  155.             IShow(ourHandle,i)
  156.         else
  157.             IHide(ourHandle,i);
  158.         IInvalidate(ourHandle, i);
  159.     END;
  160. END;
  161.  
  162. PROCEDURE DrawSampleItem(ourHandle: SampleHdl; item: integer);
  163. VAR    itemRect:    Rect;
  164.     s:            Str255;
  165. BEGIN
  166.     IGetRect(ourHandle, item, itemRect);
  167.     with ourHandle^^ do BEGIN
  168.         SetPort(dlgPtr);
  169.         if item = iHandled then
  170.             NumToString(msgHandled, s)
  171.         else
  172.             NumToString(msgIgnored, s);
  173.     END;
  174.     with itemRect do
  175.         MoveTo(left, bottom);
  176.     TextMode(srcCopy);
  177.     DrawString(s);
  178.     TextMode(srcOr);
  179. END;
  180.  
  181. FUNCTION IGetCtlHand(ourHandle: SampleHdl; item: integer): ControlHandle;
  182. VAR    itemHand:    Handle;
  183.     itemRect:    Rect;
  184.     itemType:    integer;
  185. BEGIN
  186.     with ourHandle^^ do
  187.         GetDItem(dlgPtr, item + dlgItems, itemType, itemHand, itemRect);
  188.     IGetCtlHand := ControlHandle(itemHand);
  189. END;
  190.  
  191. PROCEDURE IGetRect(ourHandle: SampleHdl; item: integer; var itemRect: Rect);
  192. VAR    itemType:    integer;
  193.     itemHand:    Handle;
  194. BEGIN
  195.     with ourHandle^^ do
  196.         GetDItem(dlgPtr, item + dlgItems, itemType, itemHand, itemRect);
  197. END;
  198.  
  199. PROCEDURE IHide(ourHandle: SampleHdl; item: integer);
  200. BEGIN
  201.     with ourHandle^^ do
  202.         HideDItem(dlgPtr, item+dlgItems);
  203. END;
  204.  
  205. PROCEDURE IShow(ourHandle: SampleHdl; item: integer);
  206. BEGIN
  207.     with ourHandle^^ do
  208.         ShowDItem(dlgPtr, item+dlgItems);
  209. END;
  210.  
  211. PROCEDURE IInvalidate(ourHandle: SampleHdl; item: integer);
  212. VAR    itemRect:    Rect;
  213. BEGIN
  214.     IGetRect(ourHandle, item, itemRect);
  215.     EraseRect(itemRect);
  216.     InvalRect(itemRect);
  217. END;
  218.  
  219. END. { of implementation }
  220.